package client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import message.Order;

public class LoadTest {

	static int LIVE_ORDERS_NUMER = 500_000;

	public static void main(String[] args) throws UnknownHostException,
			IOException {

		Socket requestSocket;
		requestSocket = new Socket("localhost", 2013);
		new Thread(new SocketReader(requestSocket)).start();
		SocketWriter sw = new SocketWriter(requestSocket, "client1");
		sw.initForLoadTest();
		sw.sendLogin("client1");

		
		long time = System.currentTimeMillis();
		
		// init		
		for (int i = 0; i < LIVE_ORDERS_NUMER; i++) {
			sw.sendOrder(new Order("client1", "orderBuyInit" + i, "IBM", 1, (double) i, 30));
		}

		int cycle = 0;
		System.out.println("init load time="+(System.currentTimeMillis()-time));
		sleep(5000);
		
		// fill cycles
		while (true) {
			cycle++;
			time = System.currentTimeMillis();
		for (int i = 0; i < LIVE_ORDERS_NUMER; i++) {
			sw.sendOrder(new Order("client1", "orderSell_"+cycle+"_"+i, "IBM", 2, (double) i, 30));
			sw.sendOrder(new Order("client1", "orderBuy_"+cycle+"_"+i, "IBM", 1, (double) i, 30));
		}
		System.out.println("cycle of filling="+(System.currentTimeMillis()-time));
		System.out.println("Orders sent: ="+(LIVE_ORDERS_NUMER*2));
		sleep(100);
}
	}

	
	
	static void sleep(int milisec) {
		try {
			Thread.sleep(milisec);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}

}
